package com.jn.retrofit.presenter;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;

import androidx.annotation.NonNull;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.jn.retrofit.common.WeakRrfHandler;
import com.jn.retrofit.contract.FoodContract;
import com.jn.retrofit.entity.FoodEntity;
import com.jn.retrofit.service.FoodService;
import com.jn.retrofit.utils.LogUtils;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class FoodPresenter implements FoodContract.IFoodPresenter {

    public static final int HTTP_SUCCESS = 0;
    public static final int HTTP_FAIL = 1;
    public static final int HTTP_SUCCESS2 = 2;

    private FoodContract.IFoodView mMainView;
    private Context mContext;

    public FoodPresenter(FoodContract.IFoodView mMainView, Context mContext) {
        this.mMainView = mMainView;
        this.mContext = mContext;
    }

    @Override
    public void getData(String keyWord) {//get方式异步请求
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://shanhe.kim/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        FoodService foodService = retrofit.create(FoodService.class);
        foodService.getData(keyWord).enqueue(new retrofit2.Callback<ResponseBody>() {
            @Override
            public void onResponse(retrofit2.Call<ResponseBody> call, retrofit2.Response<ResponseBody> response) {
                try {
                    String body = response.body().string();
                    String url = call.request().url().toString();
                    LogUtils.d("asdf", "url : " + url);
                    LogUtils.d("asdf", "onResponse : " + body);
                    Message message = Message.obtain();
                    message.obj = new String[]{body, url};
                    message.what = HTTP_SUCCESS;
                    mHandler.sendMessage(message);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }

            @Override
            public void onFailure(retrofit2.Call<ResponseBody> call, Throwable throwable) {
                LogUtils.d("asdf", "onFailure");
            }
        });

        /*foodService.getData2(keyWord).enqueue(new Callback<FoodEntity>() {
            @Override
            public void onResponse(Call<FoodEntity> call, Response<FoodEntity> response) {
                FoodEntity foodEntity = response.body();
                Message message = Message.obtain();
                message.obj = foodEntity;
                message.what = HTTP_SUCCESS2;
                mHandler.sendMessage(message);
            }

            @Override
            public void onFailure(Call<FoodEntity> call, Throwable throwable) {

            }
        });*/

        /*Map<String, Object> map = new HashMap<String, Object>();
        map.put("food", keyWord);
        foodService.getData3(map).enqueue(new Callback<FoodEntity>() {
            @Override
            public void onResponse(Call<FoodEntity> call, Response<FoodEntity> response) {
                FoodEntity foodEntity = response.body();
                Message message = Message.obtain();
                message.obj = foodEntity;
                message.what = HTTP_SUCCESS2;
                mHandler.sendMessage(message);
            }

            @Override
            public void onFailure(Call<FoodEntity> call, Throwable throwable) {

            }
        });*/
    }

    WeakRrfHandler<FoodPresenter> mHandler = new WeakRrfHandler<>(Looper.getMainLooper(), this, new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message msg) {
            int result = msg.what;
            switch (result) {
                case HTTP_SUCCESS:
                    String[] data = (String[]) msg.obj;
                    if (data == null || data.length < 2) {
                        return false;
                    }
                    String jsonStr = data[0];
                    Gson gson = new Gson();
                    try {
                        FoodEntity foodEntity = gson.fromJson(jsonStr, FoodEntity.class);
                        if (mMainView != null) {
                            mMainView.showData(foodEntity);
                        }
                    } catch (JsonSyntaxException e) {
                        LogUtils.d("asdf", "result not json");
                    }
                    break;
                case HTTP_SUCCESS2:
                    if (mMainView != null) {
                        mMainView.showData((FoodEntity) msg.obj);
                    }
                    break;
                case HTTP_FAIL:
                    break;
            }
            return false;
        }
    });
}
